package cuit.game.service;



import cuit.game.dao.ContestRespository;
import cuit.game.entity.Contest;

import jakarta.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Transactional
public class ContestService {

    @Autowired
     ContestRespository contestRespository;

    /**
     * 查找所有
     * 
     * @return
     */
    public List<Contest> findAll()  {
	return contestRespository.findAll();
    }

    /**
     * 分页查找
     * 
     * @return
     */
    public  Page<Contest> findAllByPage(int pageIndex, int pageSize) {
	//排序
	Sort sort = Sort.by(Sort.Direction.ASC, "id");
	//分页
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	//分页查询
	Page<Contest> contestPage = contestRespository.findAll(pageable);
	return contestPage;
    }
 
    /**
     * 新增
     * 
     * @param contest
     * @return
     */
    public void add(Contest contest) {
	contestRespository.save(contest);
    }
    
    /**
     * 查找某条记录
     * 
     * @param id
     * @return
     */
    public Contest findById(long id) {
	return contestRespository.findById(id).orElse(new Contest());
    }

    /**
     * 删除某条记录
     * 
     * @param id
     */
    public void deleteById(long id) {
    	contestRespository.deleteById(id);
    }


}
